package com.study.d012Components.listview;

import java.util.Comparator;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

/**
*@description 使用SimpleStringProperty类型测试集合的各种操作
*@author xtayhame
*date 2020年3月13日
*version
*since v1.0
*/
public class ListViewEventWithProperty extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		ObservableList<SimpleStringProperty> datas=FXCollections.observableArrayList();
		SimpleStringProperty s1=new SimpleStringProperty("A");
		SimpleStringProperty s2=new SimpleStringProperty("D");
		SimpleStringProperty s3=new SimpleStringProperty("C");
		SimpleStringProperty s4=new SimpleStringProperty("B");
		datas.addAll(s1,s2,s3,s4);


		Button b1  = new Button("点击");
		ListView<SimpleStringProperty> listView = new ListView<>(datas);
		listView.setPrefSize(200, 300);
		anchorPane.getChildren().addAll(b1,listView);
		AnchorPane.setLeftAnchor(listView, 50.0);
		AnchorPane.setTopAnchor(listView, 50.0);

		stage.setScene(new Scene(anchorPane, 400, 500));
		stage.show();


		javafx.util.Callback<ListView<SimpleStringProperty>,ListCell<SimpleStringProperty>> cellback =TextFieldListCell.forListView(new StringConverter<SimpleStringProperty>() {
			@Override
			public String toString(SimpleStringProperty object) {
				// TODO Auto-generated method stub
				return object.get();
			}

			@Override
			public SimpleStringProperty fromString(String string) {
				// TODO Auto-generated method stub
				return null;
			}
		});


		listView.setCellFactory(cellback);


		datas.addListener(new ListChangeListener<SimpleStringProperty>() {

			@Override
			public void onChanged(javafx.collections.ListChangeListener.Change<? extends SimpleStringProperty> c) {
				if(c.next()){
					if(c.wasAdded()){
						System.out.println("新增动作");
					}
					if(c.wasPermutated()){
						System.out.println("排序动作");
					}
					if(c.wasRemoved()){
						System.out.println("移除动作");
					}
					if(c.wasReplaced()){
						System.out.println("替换动作");
					}
					if(c.wasUpdated()){
						System.out.println("更新动作");
					}
				}
			}
		});


		b1.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				//datas.set(0, new SimpleStringProperty("abcdef"));    //新增、移除、替换
				//datas.add(new SimpleStringProperty("abcdef"));   //新增
				//datas.remove(0);   // 移除


				//排序
				/*datas.sort(new Comparator<SimpleStringProperty>() {

					@Override
					public int compare(SimpleStringProperty o1, SimpleStringProperty o2) {
						// TODO Auto-generated method stub
						return o2.get().compareTo(o1.get());
					}
				});*/


				//注意：如果直接使用s1.set()方法进行更新操作，则需要调用刷新方法 refresh()
				s1.set("大地");
				listView.refresh();

			}
		});
	}

}
